<template>
	<view class="page">
		<view class="f-column container">
			<view style="height: 20vh;"></view>
			<view class="login-title">
				<span class="thin-title">欢迎登录</span>
				<span class="bold-title bold">顺博云购商户版</span>
			</view>
			<view class="login-message f-column">
				<view class="message-item-container f-row"
					:style="focustype == 'account' ? 'border-bottom: 2rpx solid #0295fc' : ''">
					<image class="message-icon" id="test" mode="aspectFill"
						:src="(account !== '' && account.trim() !== '') ? '../../static/icons/login/account-fill.png' : '../../static/icons/login/account.png' ">
					</image>
					<input :data-name="'account'" confirm-type="done" type="text" class="message-input"
						v-model="account" placeholder="请输入用户名" placeholder-class="placeholder-class" maxlength="18"
						@focus="inputFocus" @click.stop>
				</view>
				<view class="message-item-container f-row"
					:style="focustype == 'password' ? 'border-bottom: 2rpx solid #0295fc' : ''">
					<image class="message-icon" mode="aspectFill"
						:src="(password !== '' && password.trim() !== '') ? '../../static/icons/login/password-fill.png' : '../../static/icons/login/password.png' ">
					</image>
					<input :data-name="'password'" confirm-type="done" type="text" class="message-input"
						v-model="password" placeholder="请输入密码" placeholder-class="placeholder-class" maxlength="18"
						:password="pwdIsVisible" @focus="inputFocus" @click.stop>
					<image class="visible-icon" mode="aspectFit"
						:src="pwdIsVisible ? '../../static/icons/login/unvisible.png' : '../../static/icons/login/visible.png'"
						@click="changeVisible"></image>
				</view>
				<view class="message-item-container-vcode f-row"
					:style="focustype == 'verification' ? 'border-bottom: 2rpx solid #0295fc' : ''">
					<image class="message-icon" mode="aspectFill"
						:src="(verification !== '' && verification.trim() !== '') ? '../../static/icons/login/verification-fill.png' : '../../static/icons/login/verification.png' ">
					</image>
					<input :data-name="'verification'" confirm-type="done" type="text" class="message-input-vcode"
						v-model="verification" placeholder="请输入验证码" placeholder-class="placeholder-class" maxlength="4"
						@focus="inputFocus" @click.stop>
					<image class="verification-pic" mode="aspectFill" :src="verificationCode"
						@click="getVerificationCode"></image>
				</view>
			</view>
			<view class="login-button" @click="login">登录</view>
		</view>
	</view>
</template>

<script>
	import {
		showToast,
		isEmptyStr,
		jsonParse
	} from '../../lib/utils.js'
	import LOGIN from '../../api/login.js'
	import Session from '../../lib/session.js'
	import SETTING from '../../api/setting.js'
	let _this;
	export default {
		data() {
			return {
				account: '',
				password: '',
				verification: '',
				pwdIsVisible: true,
				focustype: "",
				verificationCode: '',
				key: '',
			}
		},
		onLoad() {
			_this = this;
			// LOGIN.checkToken().then((res) => {
			// 	console.log(res);
			// 	uni.showLoading({
			// 		title: '进入中...',
			// 	});
			// 	_this.$navigate('home','switchTab', '' , () => {
			// 		uni.hideLoading();
			// 	});
			// }).catch((err) => {
			// 	console.log(err);
			// });
			_this.getVerificationCode();
		},
		methods: {
			changeVisible() {
				_this.pwdIsVisible = _this.pwdIsVisible ? false : true;
			},
			inputFocus(e) {
				_this.focustype = e.target.dataset.name;
			},
			cancelfocus() {
				if (_this.focustype == '') return;
				_this.focustype = '';
			},
			login() {
				if (isEmptyStr(_this.account)) {
					showToast('用户名不能为空');
					return;
				} else if (isEmptyStr(_this.password)) {
					showToast('密码不能为空');
					return;
				} else if (isEmptyStr(_this.verification)) {
					showToast('验证码不能为空');
					return;
				} else {
					LOGIN.login({
						account: _this.account,
						password: _this.password,
						code: _this.verification,
						key: _this.key
					}).then((res) => {
						console.log(res);
						showToast('登录成功');
						_this.$store.dispatch('LOGIN' ,res);
						_this.$navigate('home','switchTab');
					}).catch((err) => {
						showToast(err.message);
						console.log(err);
					});
				}
			},
			getVerificationCode() {
				LOGIN.Captcha().then((res) => {
					console.log(res);
					_this.verificationCode = res.captcha;
					_this.key = res.key;
				}).catch((err) => {
					console.log(err);
					showToast('验证码获取失败');
				})
			}
		}
	}
</script>

<style lang="scss">
	.container {
		margin: 0 60rpx;
		flex: 1;
	}

	.login-title {
		margin-bottom: 60rpx;

		.thin-title {
			font-size: 28rpx;
			margin-right: 15rpx;
		}

		.bold-title {
			font-size: 50rpx;
			color: #2c6ffe;
		}
	}

	.login-message {
		width: 100%;

		.message-item-container {
			box-sizing: border-box;
			height: 70rpx;
			margin: 50rpx 0 0 0;
			border-bottom: 2rpx solid #d8d8d8;
			background-color: #FFFFFF;

			.message-input {
				height: 50rpx;
				margin: 10rpx;
				flex: 1;
				font-size: 24rpx;
			}

			.message-icon,
			.visible-icon {
				height: 30rpx;
				width: 30rpx;
				margin: 15rpx;
			}
		}

		.message-item-container-vcode {
			box-sizing: border-box;
			height: 70rpx;
			margin: 50rpx 0 0 0;
			background-color: #ffffff;
			display: flex;
			flex-direction: row;
			align-items: center;
			border-bottom: 2rpx solid #d8d8d8;
			overflow: hidden;
			white-space: nowrap;

			.message-input-vcode {
				height: 70rpx;
				line-height: 70rpx;
				flex: 1;
				font-size: 24rpx;
				padding-left: 15rpx;
			}

			.verification-pic {
				width: 200rpx;
				height: 66rpx;
			}

			.message-icon {
				height: 30rpx;
				width: 30rpx;
				margin: 15rpx;
			}
		}
	}

	.login-button {
		height: 80rpx;
		width: 100%;
		background-color: #2c6ffe;
		border-radius: 40rpx;
		line-height: 80rpx;
		color: #FFFFFF;
		text-align: center;
		font-size: 28rpx;
		margin-top: 100rpx;
	}
</style>
